package com.buss.product.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.buss.base.model.DicData;
import com.buss.base.service.DicDataService;
import com.buss.core.controller.BaseController;
import com.buss.core.dic.RecordStatus;
import com.buss.core.exception.ServiceException;
import com.buss.core.exception.WebException;
import com.buss.core.persistence.OrderBy;
import com.buss.core.persistence.Pagination;
import com.buss.core.persistence.ResultList;
import com.buss.core.sysparam.SysStaticParam;
import com.buss.core.utils.StringUtil;
import com.buss.product.model.CateProp;
import com.buss.product.model.CateProp.PropEntryWay;
import com.buss.product.model.CateProp.PropSelectType;
import com.buss.product.model.CatePropGroup;
import com.buss.product.service.CatePropGroupService;
import com.buss.product.service.CatePropService;

@Controller
@RequestMapping("/admin/cateprop")
public class CatePropController extends BaseController {

	@Autowired
	private CatePropService propService;
	
	@Autowired
	private CatePropGroupService groupService;
	
	@Autowired
	private DicDataService dicService;
	@Override
	public ModelAndView listPage(String menuid) {
		return null;
	}

	@RequestMapping("/{type}")
	public ModelAndView editPage(@PathVariable String type, Long id,Long groupId,HttpServletRequest request) {
		ModelAndView view = new ModelAndView("productprop/propedit");
		CateProp prop = new CateProp();
		if(EDIT_PAGE.equals(type)){			
			prop = propService.loadProp(id);
			if(prop.getEntryWay().equals(PropEntryWay.dicChoice)){
				DicData dic = dicService.loadDicData(getCompanyId(request), prop.getDicCode());
				prop.setDicName(dic.getName());
			}
			prop.getPropGroup();
		}else{
			CatePropGroup group = groupService.loadPropGroup(groupId);
			prop.setPropGroup(group);
			prop.setSelectType(PropSelectType.uniqueProp);
			prop.setEntryWay(PropEntryWay.manualWay);
			prop.setStatus(RecordStatus.normal);
			prop.setListVisible(false);
			prop.setIsSearch(false);
			prop.setStatisVisible(false);
		}
		view.addObject("type", type);
		view.addObject("prop", prop);
		return view;
	}

	@Override
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String, Object> delete(Long[] ids) throws WebException {
		propService.removeProp(ids);		
		return SysStaticParam.resultMessage("删除属性信息成功!");
	}

	@RequestMapping("/add")
	@ResponseBody
	public Map<String,Object> add( CateProp prop,HttpServletRequest request) throws WebException{
		try {
			prop.getPropGroup().setCompanyId(getCompanyId(request));
			prop = propService.saveProp(prop);
		} catch (ServiceException e) {
			throw new WebException(e.getMessage());
		}
		Map<String, Object> map = SysStaticParam.resultMessage("新增属性信息成功!");
		map.put("prop", prop);
		return map;
	}
	
	@RequestMapping("/edit")
	@ResponseBody
	public Map<String,Object> edit(CateProp prop,HttpServletRequest request){
		prop.getPropGroup().setCompanyId(getCompanyId(request));
		propService.editProp(prop);
		return SysStaticParam.resultMessage("修改属性信息成功!");
	}
	
	@RequestMapping("/list/{groupId}")
	@ResponseBody
	public Map<String,Object> list(@PathVariable Long groupId,String name,Boolean isSearch,RecordStatus status,
			String orderField,String orderType, Pagination page){
		OrderBy orderBy = null;
		if(StringUtil.isNotEmpty(orderField) && StringUtil.isNotEmpty(orderType)){
			orderBy = new OrderBy();
			orderBy.add(orderField, orderType);
		}
		ResultList<CateProp> rs = propService.queryProp(groupId, name, isSearch, status, orderBy, page);
		Map<String, Object> map = SysStaticParam.resultMessage("获取属性信息成功!");
		map.put(SysStaticParam._RESULT, rs);
		return map;
	}

	@Override
	public ModelAndView editPage(String type, Long id) {
		// TODO Auto-generated method stub
		return null;
	}
}
